package com.gugee.ins.common.dal.mapper.blogger;

import com.gugee.common.mapper.GugeeMapper;
import com.gugee.ins.common.dto.InsBloggerFollowerDto;
import com.gugee.ins.common.model.mysql.blogger.InsBlogger;
import com.gugee.ins.common.model.spider.blogger.InsBloggerUserFromId;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface InsBloggerMapper extends GugeeMapper<InsBlogger> {
    @Select("select max(id) from ins_blogger ")
    long getMaxId();
    @Select("select uid,id  from ins_blogger where id between #{startId} and #{endId} and account_status = 0  ")
    List<InsBloggerMessage> findInsBloggerMessageByPagesId(long startId,long endId);
    @Select("select uid,id  from ins_blogger where id between #{startId} and #{endId} AND follower_count < #{followerCount} AND last_collect_time < #{lastCollectTime} and account_status = 0  ")
    List<InsBloggerMessage> findInsBloggerMessageByPages(long startId,long endId,long followerCount,long lastCollectTime);
    @Select("select a.uid,a.id  from ins_blogger a where a.id between #{startId} and #{endId} AND a.follower_count >= #{followerCount} and a.account_status = 0  AND not exists (select 1 from ins_blogger_main b where b.uid=a.uid) ")
    List<InsBloggerMessage> findInsMBloggerFromInsBlogger(long startId,long endId,long followerCount);

    /**
     * 根据uid查找数据
     * @param uid
     * @return
     */
    @Select("select * from ins_blogger where uid = #{uid}")
    InsBlogger findByUid(long uid);

    /**
     * 查询存在的uid
     * @param uids
     * @return
     */
    @Select("<script>"
            +"select id,uid,follower_count,is_zombie from ins_blogger where uid in "
            + "<foreach item='uid' index='index' collection='uids'      open='(' separator=',' close=')'>"
            + "#{uid}"
            + "</foreach>"
            + "</script>")
    List<InsBlogger> findExistUidByUids(@Param("uids") List<Long> uids);

    /**
     * 查询指定时间内是否更新过的uids
     * @param uids
     * @return
     */

    @Select("<script>"
            +"select uid from ins_blogger where uid in "
            + "<foreach item='uid' index='index' collection='uids'      open='(' separator=',' close=')'>"
            + "#{uid}"
            + "</foreach>"
            + " AND last_collect_time>#{collectTime} "
            + "</script>")
    List<Long> findExistUidByUidsWithCollectTime(@Param("uids") List<Long> uids,long collectTime);
    /**
     * 根据条件获取真实粉丝或僵尸粉丝列表
     * @return
     */
    @Select({"<script>",
            "select b.* from ins_rel_blogger_follower rb left join ins_blogger b on rb.f_uid = b.uid where rb.uid = #{uid} and b.last_collect_time > 0 "
            ,"<when test='isZombie==true'>"
            ," and b.is_zombie = #{isZombie} or b.is_massive_following = true"
            ,"</when>"
            ,"<when test='isZombie==false'>"
            ," and b.is_zombie = #{isZombie} or b.is_massive_following = false",
            "</when>"
            ,"</script>"})
    List<InsBlogger> findByFollower(long uid, boolean isZombie);

    @Select({"<script>",
            "select count(*) from ins_rel_blogger_follower rb left join ins_blogger b on rb.f_uid = b.uid where rb.uid = #{uid} and b.last_collect_time > 0 "
            ,"<when test='isZombie==true'>"
            ," and b.is_zombie = #{isZombie} or b.is_massive_following = true"
            ,"</when>"
            ,"<when test='isZombie==false'>"
            ," and b.is_zombie = #{isZombie} or b.is_massive_following = false",
            "</when>"
            ,"</script>"})
    long countByFollower(long uid,boolean isZombie);

    /**
     * 根据 帖子 shortcode 查找标记用户及其账户类型
     * @param shortcode
     * @return
     */
    @Select("SELECT t1.tagger_uid uid, IFNULL(t2.id,0) id , IFNULL(case when t2.last_collect_time = 0 then 2 else t2.is_business end,0) type_code " +
            "FROM ( SELECT tagger_uid FROM ins_rel_media_tagger_user WHERE shortcode = #{shortcode} ) t1 " +
            "LEFT JOIN ins_blogger t2 ON t1.tagger_uid = t2.uid")
    List<InsBloggerMessage> findInsBloggerByShortcode(String shortcode);

    /**
     * 根据uid获取粉丝数等信息
     * @param uids
     * @return
     */
    @Select("<script>" +
            " SELECT id,uid,follower_count,account_status from ins_blogger where uid " +
            "<foreach item='uid' index='index' collection='uids'      open='(' separator=',' close=')'>" +
            "#{uid}" +
            "</foreach>" +
            "</script>")
    List<InsBloggerFollowerDto> findFollowerCountByUid(List<Long> uids);
}